package org.agilar.gwt.sortabletable;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class TableModel
{
	protected List<String> columnNames = new ArrayList<String>();
	protected Set<TableModelListener> listeners = new HashSet<TableModelListener>();

	protected int columns;
	
	public TableModel(int columns)
	{
		this.columns = columns;
		for (int i=0; i<columns; i++)
		{
			columnNames.add("");
		}
	}
	
	public void setColumnName(int column, String name)
	{
		columnNames.set(column, name);
	}

	public abstract Object get(int row, int column);
	public abstract int getRowCount();
	public String getColumnName(int column)
	{
		return columnNames.get(column);
	}
	
	public abstract boolean isColumnSortable(int column);

	public abstract void sort(int column, boolean ascending);

	public int getColumnCount()
	{
		return columns;
	}

	public void addTableModelListener(TableModelListener listener)
	{
		listeners.add(listener);
	}

	public void fireModelChanged()
	{
		for (TableModelListener listener : listeners)
		{
			listener.modelChanged();
		}
	}

	public void removeTableModelListener(TableModelListener listener)
	{
		listeners.remove(listener);
	}
}
